﻿Imports System.Windows.Forms
Imports MySql.Data.Types
''' <summary>
''' Table_Adapter de Sobres
''' </summary>
''' <remarks></remarks>

Public Class Cabecera_SobreTableAdapter
    Inherits DSEATableAdapters.Cabecera_SobreTableAdapter

    Public Sub Guardar(ByVal DT As DSEA.Cabecera_SobreDataTable)
        Try
            Me.Update(DT)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub Guardar(ByVal DR As DSEA.Cabecera_SobreRow)
        Try
            Me.Update(DR)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Function Nuevo_Sobre(ByVal DT As DSEA.Cabecera_SobreDataTable, ByVal Socio As String) As Boolean

        Try
            Dim TA_Aux_Socio As New DSEATableAdapters.SociosTableAdapter
            Dim Dr_Sobre As DSEA.Cabecera_SobreRow = TryCast(DT.NewRow, DSEA.Cabecera_SobreRow)
            Dim Dt_Socio As New DSEA.SociosDataTable

            TA_Aux_Socio.FillBySocio(Dt_Socio, Socio)

            Dr_Sobre.Sobre = Me.Nuevo_Id_Sobre
            Dr_Sobre.Socio = Socio
            Dr_Sobre.Nombre_Socio = Dt_Socio.Rows(0).Item(Dt_Socio.NombreColumn.ToString)
            Dr_Sobre.Personas = 0
            Dr_Sobre.Situacion_Sobre = "C"
            Dr_Sobre.Tipo_Situacion = "Consumiendo"
            Dr_Sobre.Total_Gasto = 0
            Dr_Sobre.Fecha_Sobre = DateTime.Now()

            DT.AddCabecera_SobreRow(Dr_Sobre)

            Return True

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Public Sub Pagar_Sobre(ByRef DR As DSEA.Cabecera_SobreRow)
        Try
            DR.Situacion_Sobre = "P"
            DR.Fecha_Cobro = Now
            Me.Guardar(DR)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub Pagar_Por_Cuenta(ByRef DR As DSEA.Cabecera_SobreRow)
        Try
            DR.Situacion_Sobre = "D"

            Me.Guardar(DR)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub Borrar_sobre(ByRef DR As DSEA.Cabecera_SobreRow)
        Try
            DR.Situacion_Sobre = "B"
            Me.Guardar(DR)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub Repaso_Administracion(ByRef DT As DSEA.Cabecera_SobreDataTable)
        Try
            For Each Dr As DSEA.Cabecera_SobreRow In DT
                If Dr.Aceptar Then
                    Dr.Aprobar()
                ElseIf Dr.Rechazar Then
                    Dr.Rechaza()
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Partial Class DSEA
    Partial Class Cabecera_SobreDataTable
        Private Sub Cabecera_SobreDataTable_ColumnChanged(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanged
            Try
                Dim Dr As DSEA.Cabecera_SobreRow = e.Row

                If e.Column.ColumnName = Me.AceptarColumn.ColumnName Then
                    If Dr.Aceptar And Dr.Rechazar Then
                        Dr.Rechazar = False
                    End If
                ElseIf e.Column.ColumnName = Me.RechazarColumn.ColumnName Then
                    If Dr.Aceptar And Dr.Rechazar Then
                        Dr.Aceptar = False
                    End If
                End If
            Catch ex As Exception

            End Try
        End Sub

        Private Sub Cabecera_SobreDataTable_TableNewRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) Handles Me.TableNewRow
            Try
                Dim DR_Aux As DSEA.Cabecera_SobreRow = TryCast(e.Row, DSEA.Cabecera_SobreRow)

                DR_Aux.Gasto_Cocina = False
                DR_Aux.Personas = 1
            Catch ex As Exception
                Console.Write(ex.Message)
            End Try

        End Sub
    End Class

    Partial Class Cabecera_SobreRow
        Public Sub Recalcula_Total(ByVal DT_Linea_Sobre As DSEA.Linea_SobreDataTable)

            Me.Total_Gasto = 0
            Me.Gasto_Por_Persona = 0

            For Each Dr As DSEA.Linea_SobreRow In DT_Linea_Sobre
                Me.Total_Gasto = Format(Me.Total_Gasto + Dr.Total_Linea, "###.00")
            Next

            If Not Me.IsPersonasNull And Me.Personas <> 0 And Me.Gasto_Cocina Then
                Me.Gasto_Por_Persona = Format(Me.Total_Gasto / Me.Personas, "###.0") & " Euros por persona"
            End If
        End Sub

        Public Sub Pagar()
            If Me.Situacion_Sobre = "D" Then
                Me.Situacion_Sobre = "P"
            End If
        End Sub

        Public Sub Aprobar()
            Try
                Me.Situacion_Sobre = "O"
                Me.Fecha_Cobro = DateTime.Now
            Catch ex As Exception

            End Try
        End Sub

        Public Sub Rechaza()
            Try
                Me.Situacion_Sobre = "I"
            Catch ex As Exception

            End Try
        End Sub
    End Class
End Class
